package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.ClickJourneyPersonActivity;
import com.itcast.donkey.modles.ClickJourneyModles;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

/**
 * Created by lanouhn on 16/9/2.
 */
public class ClickJourneyAdapter extends RecyclerView.Adapter<ClickJourneyAdapter.ClickJourneyViewHolder> {

    private Context context;
    private ClickJourneyModles modles;
    private int count = 0;
    private String name;

    public ClickJourneyAdapter(Context context, ClickJourneyModles modles, int count, String name) {
        this.context = context;
        this.modles = modles;
        this.count = count;
        this.name = name;
    }

    @Override
    public ClickJourneyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_click_journey, null);
        ClickJourneyViewHolder holder = new ClickJourneyViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ClickJourneyViewHolder holder, final int position) {
        if (position == 0) {
            holder.tvItemClickJourneyCount.setVisibility(View.VISIBLE);
            holder.linClickJourney.setVisibility(View.VISIBLE);

            holder.tvItemClickJourneyCount.setText(count + "篇游记");
            holder.tvClickJourneyDate.setText(modles.getAttraction_trips().get(position).getTrip().getStart_date() +
                    "@" + modles.getAttraction_trips().get(position).getTrip().getUser().getName());
            holder.tvClickJourneyTitle.setText(modles.getAttraction_trips().get(position).getComment());
            //动态添加图片
            for (int i = 0; i < modles.getAttraction_trips().get(position).getNotes().size(); i++) {
                LinearLayout ll = new LinearLayout(context);
                ll.setOrientation(LinearLayout.VERTICAL);
                ImageView image = new ImageView(context);
                TextView textView = new TextView(context);
                ImageOptions imageOptions = new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))//图片大小
                        .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                        .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                        .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                        .setUseMemCache(true)//缓存
                        .setIgnoreGif(false)//不忽略动态图
                        .build();
                x.image().bind(image, modles.getAttraction_trips().get(position).getNotes().get(i).getPhoto_url(), imageOptions);
                textView.setText(modles.getAttraction_trips().get(position).getNotes().get(i).getDescription());
                textView.setWidth(100);
                textView.setLines(1);
                ll.addView(image);
                ll.addView(textView);
                holder.linClickJourneyImage.addView(ll);

                holder.tvClickJourneyTitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ClickJourneyPersonActivity.class);
                        intent.putExtra("modles", modles.getAttraction_trips().get(position));
                        intent.putExtra("name1", name);
                        context.startActivity(intent);
                    }
                });
                holder.tvClickJourneyDate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ClickJourneyPersonActivity.class);
                        intent.putExtra("modles", modles.getAttraction_trips().get(position));
                        intent.putExtra("name1", name);
                        context.startActivity(intent);
                    }
                });
            }
        } else {
            holder.tvClickJourneyDate.setText(modles.getAttraction_trips().get(position).getTrip().getStart_date() +
                    "@" + modles.getAttraction_trips().get(position).getTrip().getUser().getName());
            holder.tvClickJourneyTitle.setText(modles.getAttraction_trips().get(position).getComment());
            for (int i = 0; i < modles.getAttraction_trips().get(position).getNotes().size(); i++) {
                LinearLayout ll = new LinearLayout(context);
                ll.setOrientation(LinearLayout.VERTICAL);
                ImageView image = new ImageView(context);
                TextView textView = new TextView(context);
                ImageOptions imageOptions = new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))//图片大小
                        .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                        .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                        .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                        .setUseMemCache(true)//缓存
                        .setIgnoreGif(false)//不忽略动态图
                        .build();
                x.image().bind(image, modles.getAttraction_trips().get(position).getNotes().get(i).getPhoto_url(), imageOptions);
                textView.setText(modles.getAttraction_trips().get(position).getNotes().get(i).getDescription());
                textView.setWidth(100);
                textView.setLines(1);
                ll.addView(image);
                ll.addView(textView);
                holder.linClickJourneyImage.addView(ll);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ClickJourneyPersonActivity.class);
                    intent.putExtra("modles", modles.getAttraction_trips().get(position));
                    intent.putExtra("name1", name);
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return modles.getAttraction_trips().size();
    }

    public class ClickJourneyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvItemClickJourneyCount;
        private LinearLayout linClickJourney;
        private TextView tvClickJourneyTitle;
        private HorizontalScrollView hroClickJourney;
        private LinearLayout linClickJourneyImage;
        private TextView tvClickJourneyDate;

        public ClickJourneyViewHolder(View itemView) {
            super(itemView);
            tvItemClickJourneyCount = (TextView) itemView.findViewById(R.id.tv_item_click_journey_count);
            linClickJourney = (LinearLayout) itemView.findViewById(R.id.lin_click_journey);
            tvClickJourneyTitle = (TextView) itemView.findViewById(R.id.tv_click_journey_title);
            hroClickJourney = (HorizontalScrollView) itemView.findViewById(R.id.hro_click_journey);
            linClickJourneyImage = (LinearLayout) itemView.findViewById(R.id.lin_click_journey_image);
            tvClickJourneyDate = (TextView) itemView.findViewById(R.id.tv_click_journey_date);

        }
    }
}
